package com.itheima.web.manager;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.db.Admin;
import com.itheima.service.db.AdminService;
import com.itheima.service.mongo.MovementService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.web.exception.BusinessException;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.Duration;
import java.util.*;

@Component
public class AdminManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private MovementService movementService;

    /**
     * 生成验证码
     *
     * @param uuid 前台传来的用户的唯一标识
     * @return 图形验证码
     */
    public LineCaptcha sendCode(String uuid) {
        //使用hutool工具包生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        //获取验证码
        String code = lineCaptcha.getCode();
        System.out.println("验证码:" + code);
        //将验证码存入redis中,五分钟有效期
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE + uuid, code, Duration.ofMinutes(5));
        return lineCaptcha;
    }

    @Reference
    private AdminService adminService;

    /**
     * 管理员登录
     *
     * @param username         用户名
     * @param password         密码
     * @param verificationCode 验证码
     * @param uuid             uuid
     * @return token
     */
    public String login(String username, String password, String verificationCode, String uuid) throws BusinessException {
        //1.获取redis中存的验证码
        String code = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);
        //2.对比输入的验证码和存的是否一致
        if (!StrUtil.equals(code, verificationCode)) {
            throw new BusinessException("验证码输入错误");
        }
        //根据用户名查询用户
        Admin admin = adminService.findAdminByName(username);
        if (admin == null) {
            throw new BusinessException("用户名输入错误");
        }
        //将接收到的密码进行加密
        String md5 = SecureUtil.md5(password);
        if (!StrUtil.equals(md5, admin.getPassword())) {
            throw new BusinessException("密码输入错误");
        }
        //构建token数据
        Map<String, Object> map = new HashMap<>();
        map.put("id", admin.getId());
        map.put("username", admin.getUsername());
        String token = JwtUtil.createToken(map);
        //登录成功验证码没有存在的必要了
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE + uuid);
        //将token存入redis中
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, "1", Duration.ofDays(30));
        return token;
    }

    /**
     * 查询用户个人信息
     *
     * @param admin 管理员对象
     * @return
     */
    public Admin findAdminInfo(Admin admin) {
        return adminService.finById(admin.getId());
    }

    /**
     * 用户登出
     *
     * @param token token
     */
    public Admin logout(String token) {
        //删除token数据
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN + token);
        return null;
    }

    //动态人工拒绝
    public Map rejectMovement(String[] movementIds) {
        return movementService.rejectUpdateMovement(movementIds);
    }
    //动态人工通过
    public Map passMovement(String[] movementIds) {
        return movementService.passUpdateMovement(movementIds);

    }
}
